AddCSLuaFile( "cl_init.lua" ) // Make sure clientside  
AddCSLuaFile( "shared.lua" )  // and shared scripts are sent.     
include('shared.lua')     

function ENT:Initialize()     	
	self.Entity:SetModel( "models/props_trainstation/trainstation_clock001.mdl" )  	
	self.Entity:PhysicsInit( SOLID_VPHYSICS )      // Make us work with physics,  	
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )   // after all, gmod is a physics  	
	self.Entity:SetSolid( SOLID_VPHYSICS )         // Toolbox                	
	self.on = false
	self.angle = self.Entity:GetAngles()
	--self.angle.p = self.angle.p + 90
	self.Entity:StartMotionController()
	self.Entity:SetColor(150,0,0,255)
	self.onboard = false
	self.object = nil
	self.weld = nil
	self.used = false
end     

function ENT:Touch(activator)
if (!self.onboard) and (!self.used) then
local c = activator:GetClass()
if (c != "conventpad") and (c != "prop_ragdoll") then
if (activator:GetMoveType() == 6) and (!activator:IsPlayerHolding( )) and (!activator:IsConstrained( )) then
local mine = activator:OBBMins() 
local pos = self.Entity:GetPos() 
pos.z = pos.z + (-mine.z + 5)
activator:SetPos(pos)
activator:SetAngles(Angle(0,0,0))
self.object = activator
self.onboard = true
self.weld = constraint.Weld( self.Entity, activator, 0, 0, 0, true )
self.Entity:SetColor(0,0,150,255)
end
end
end
end

function ENT:Unload()
if(self.lp == 1) and (self.onboard) then
local r = self.dr
if (self.object:IsValid()) then
self.used = true
timer.Simple(1,unuse,self)
self.Entity:SetColor(150,0,0,255)
self.onboard = false
self.weld:Remove()
local v = Vector(0,0,0)
if (r == 0) then
v.x = self.object:GetPhysicsObject():GetMass() * 400
elseif (r == 1) then
v.x = (self.object:GetPhysicsObject():GetMass() * 400) * -1
elseif (r == 2) then
v.y = (self.object:GetPhysicsObject():GetMass() * 400) 
elseif (r == 3) then
v.y = (self.object:GetPhysicsObject():GetMass() * 400) * -1
end
v.z = self.object:GetPhysicsObject():GetMass() * 400
self.object:GetPhysicsObject():ApplyForceCenter(v)
end
end
end

 function ENT:Think()
 if (self.on) then
local d = self.Entity:GetPos():Distance(self.pos)
if d < 40 then
self:Unload()
local t = self.control:Update(self.current)
self.current = t.current
self.speed = t.speed
self.pos = t.pos
self.lp = t.lp
self.dr = t.dr
end
end
if (self.onboard) then
if (self.object:IsValid()) then
if (self.object:IsPlayerHolding()) then
self.Entity:SetColor(150,0,0,255)
self.onboard = false
self.weld:Remove()
end
else
self.Entity:SetColor(150,0,0,255)
self.onboard = false
end
end
end

  function ENT:PhysicsSimulate( phys, deltatime )
			local mpphys = {}
			local sp = self.Entity:GetPos():Distance(self.pos)/self.speed
 			mpphys.secondstoarrive	= sp
			mpphys.pos				= self.pos
			mpphys.maxangular		= 4000
			mpphys.maxangulardamp	= 10000
			mpphys.maxspeed			= 100000
			mpphys.maxspeeddamp		= 10000
			mpphys.dampfactor		= 0.8
			mpphys.teleportdistance	= 3000
			mpphys.angle			= self.angle
			mpphys.deltatime		= deltatime
			phys:ComputeShadowControl(mpphys)
 end
 